package com.yzc.android.network.base;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.yzc.android.utils.LogUtils;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by yzc on 16/2/1.
 */
public class KDJsonObjectRequest extends KDBaseRequest<JSONObject> {

    public KDJsonObjectRequest(int method,
                               Map<String, String> params,
                               Response.Listener<JSONObject> listener,
                               Response.ErrorListener errorListener) {
        super(method, null, params, listener, errorListener);
    }

    public KDJsonObjectRequest(int method,
                               String hostUrl,
                               Map<String, String> params,
                               Response.Listener<JSONObject> listener,
                               Response.ErrorListener errorListener) {
        super(method, hostUrl, null, params, listener, errorListener);
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
            if (TextUtils.isEmpty(jsonString))
            {
                return Response.error(new ParseError(new VolleyError("json为空")));
            }
            Response<JSONObject> jsonResponse = Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));

            try {
                try {
                    String paramJson = gson.toJson(getParams());
                    LogUtils.t(loggerTag).d("method=%s%nparams=%s%nresponse=%s", getUrl(), paramJson, gson.toJson(jsonResponse.result));

                }
                catch (Exception e)
                {
                    LogUtils.t(loggerTag).d("method=%s%nresponse=%s", getUrl(), gson.toJson(jsonResponse.result));
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

            return jsonResponse;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            je.printStackTrace();
            return Response.error(new ParseError(je));
        }

    }

//    /**
//     * @deprecated Use {@link #getBodyContentType()}.
//     */
//    @Override
//    public String getPostBodyContentType() {
//        return getBodyContentType();
//    }
//
//    /**
//     * @deprecated Use {@link #getBody()}.
//     */
//    @Override
//    public byte[] getPostBody() {
//        return getBody();
//    }
//
//    @Override
//    public String getBodyContentType() {
//        return PROTOCOL_CONTENT_TYPE;
//    }
//
//    @Override
//    public byte[] getBody() {
//        try {
//            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
//        } catch (UnsupportedEncodingException uee) {
//            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
//                    mRequestBody, PROTOCOL_CHARSET);
//            return null;
//        }
//    }
}
